/*
 * @lc app=leetcode.cn id=1694 lang=javascript
 *
 * [1694] 重新格式化电话号码
 */

// @lc code=start
/**
 * @param {string} number
 * @return {string}
 */
var reformatNumber = function (number) {
  number = number.replace(/[-\s]/g, '').replace(/\d{3}\B/g, '$&-');
  const lastIndex = number.lastIndexOf('-');
  if (lastIndex === -1) {
    return number;
  }
  const len = number.length - 1 - lastIndex;
  if (len !== 1) {
    return number;
  }
  const rest = number.slice(-5).replace('-', '').replace(/\d{2}\B/g, '$&-');
  return number.slice(0, -5) + rest;
};
// @lc code=end

